//
//  NewsDetailsView.m
//  news
//
//  Created by lanou on 15/12/19.
//  Copyright © 2015年 lanou. All rights reserved.
//

#import "NewsDetailsView.h"
#import "NewsDetails.h"

@interface NewsDetailsView ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIView *bgView;

@property (nonatomic, strong) UILabel *titleLabel;

@end

@implementation NewsDetailsView

- (void)dealloc
{
    _webView.delegate = nil;
    [_webView loadHTMLString:@"" baseURL:nil];
    [_webView stopLoading];
    [_webView removeFromSuperview];
    [[NSURLCache sharedURLCache] removeAllCachedResponses];
    //    NSLog(@"%s",__func__);
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight - 55)];
        
        self.webView.backgroundColor = [UIColor whiteColor];
        
        self.webView.scrollView.showsHorizontalScrollIndicator = NO;
        
        self.webView.scrollView.contentInset = UIEdgeInsetsMake(50, 0, 0, 0);
#pragma mark - 0923clown解决了没加载好时出现黑条的bug
        self.webView.scrollView.contentOffset = CGPointMake(0, -50);
        self.webView.scrollView.delegate = self;
        [self addSubview:self.webView];
        
        self.bgView = [[UIView alloc] initWithFrame:CGRectMake(0, -50, kWidth, 50)];
        self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, kWidth - 30, 40)];
        self.titleLabel.numberOfLines = 0;
        self.titleLabel.font = [UIFont boldSystemFontOfSize:18.0];
        self.titleLabel.textColor = [UIColor blackColor];
        [self.bgView addSubview:self.titleLabel];
        [self.webView.scrollView addSubview:self.bgView];
        
    }
    return self;
}

- (void)setNewsDetails:(NewsDetails *)newsDetails
{
    if (_newsDetails != newsDetails) {
        _newsDetails = newsDetails;
        self.titleLabel.text = newsDetails.title;
        [self showInWebView:newsDetails];
    }
}


#pragma mark - HTML图文混排

//拼接HTML字符串
- (void)showInWebView:(NewsDetails *)newsDetails {
    
    
    NSMutableString *htmlStr = [NSMutableString string];
    [htmlStr appendString:@"<html>"];
    
    [htmlStr appendString:@"<head>"];
    [htmlStr appendFormat:@"<link rel=\"stylesheet\" href=\"%@\">",[[NSBundle mainBundle] URLForResource:@"ITNewsDetails.css" withExtension:nil]];
    [htmlStr appendString:@"</head>"];
    
    [htmlStr appendString:@"<body>"];
    [htmlStr appendString:[self touchBody:newsDetails]];
    [htmlStr appendString:@"</body>"];
    [htmlStr appendString:@"<div class=\"sep\"><hr /></p></p></div>"];
    
    [htmlStr appendString:@"</html>"];
    
    [self.webView loadHTMLString:htmlStr baseURL:nil];
}

//拼接body字符串
- (NSString *)touchBody:(NewsDetails *)newsDetails {
    
//    NSString *picStr = [[NSUserDefaults standardUserDefaults] objectForKey:@"pic"];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"ic_blank_collect@3x" ofType:@"png"];;
    NSString *picPath = [NSString stringWithFormat:@"file:%@",path];
    
    NSMutableString *bodyStr = [NSMutableString string];
    
    //标题+时间+分割线
    //    [bodyStr appendFormat:@"<h3>%@</h3>",model.title];//HTML标准标题格式 h1最大 h2中等 h3最小
    //    [bodyStr appendFormat:@"<div class=\"title\">%@</div>",model.title];
    [bodyStr appendFormat:@"<div class=\"time\">%@      %@</div>",[newsDetails.ptime substringFromIndex:5], newsDetails.source];
    //    [bodyStr appendString:@"<div class=\"sep\"><hr /></div>"];
    
    //图片位置
    NSString *onload = @"this.onclick = function() {"
    "  window.location.href = 'sx:src=' +this.src;"
    "};";
    
    if (newsDetails.video) {
        for (NewsDetailsVideo *video in newsDetails.video) {
#pragma mark - 0924ZJ-webView视频播放 ok
            [bodyStr appendFormat:@"<div class=\"video\" onclick='window.location.href=\"%@\"'>",video.url_mp4];
            
            //            NSLog(@"%@", videoModel.url_mp4);
            
            [bodyStr appendFormat:@"<img onload=\"%@\" width=\"%f\" height=\"%f\" src=\"%@\">", onload, kWidth * 0.96, kWidth * 0.6,video.cover];
            [bodyStr appendFormat:@"<div class=\"alt\">%@</div>", video.alt];
            [bodyStr appendString:@"</div>"];
        }
    }
    
    if (newsDetails.body) {
        [bodyStr appendFormat:@"<div class=\"article\">%@</div>", newsDetails.body];
        
        for (NewsDetailsImg *img in newsDetails.img) {
            NSMutableString *imgHtml = [NSMutableString string];
            // 设置img的div
            [imgHtml appendString:@"<div align = center class=\"img\">"];
            
            // 数组存放被切割的像素
            NSArray *pixel = [img.pixel componentsSeparatedByString:@"*"];
            CGFloat width = [[pixel firstObject]floatValue];
            CGFloat height = [[pixel lastObject]floatValue];
            // 判断是否超过最大宽度
            CGFloat maxWidth = kWidth * 0.8;
            if (width > maxWidth) {
                height = maxWidth / width * height;
                width = maxWidth;
            }
//            if ([picStr isEqualToString:@"have"]) {
            if (img.src.length) {
                [imgHtml appendFormat:@"<img onload=\"%@\" width=\"%f\" height=\"%f\" src=\"%@\">",onload,width,height,img.src];
            } else {
                [imgHtml appendFormat:@"<img onload=\"%@\" width=\"%f\" height=\"%f\" src=\"%@\">",onload,width,height,picPath];
            }
            //拼接图片简介
            [imgHtml appendFormat:@"<div class=\"alt\">%@</div>", img.alt];
            // img结束标记
            [imgHtml appendString:@"</div>"];
            // 替换标记
#pragma mark 暂时去除图片
            [bodyStr replaceOccurrencesOfString:img.ref withString:imgHtml options:NSCaseInsensitiveSearch range:NSMakeRange(0, bodyStr.length)];
        }
    }
    
    if (newsDetails.relative.count > 0) {
        [bodyStr appendString:@"<div class=\"sep\"><hr /><hr /></div>"];
        [bodyStr appendString:@"<div class=\"relative_title\">相关新闻 :</div>"];
        
        [bodyStr appendString:@"<div class=\"related\">"];
        for (NewsDetailsRelative *relative in newsDetails.relative) {
         
            [bodyStr appendFormat:@"<a href=\"%@\">%@</a></p>", relative.href, relative.title];
        }
        [bodyStr appendString:@"</div>"];
    }
    
    if (newsDetails.relative_sys.count > 0) {//推荐阅读
        [bodyStr appendString:@"<div class=\"sep\"><hr /><hr /></div>"];
        [bodyStr appendString:@"<div class=\"relative_title\">推荐阅读 :</div>"];
        
        [bodyStr appendString:@"<div class=\"related\">"];
        for (NewsDetailsRelative *relative in newsDetails.relative_sys) {
            NSString *relativeURL = [NSString stringWithFormat:@"http://c.m.163.com/nc/article/%@/full.html",relative.ID];
            [bodyStr appendFormat:@"<a href=\"%@\">%@</a></p>", relativeURL, relative.title];
        }
        [bodyStr appendString:@"</div>"];
    }
    
    return bodyStr;
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
